package com.ybt.pub;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlUtil {

    public static Document loadXml(String path, Charset encoding)
            throws Exception {
        FileInputStream fi = new FileInputStream(path);
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(new InputStreamReader(fi, encoding));
        return doc;
    }

    public static void saveXml(String path, String content, Charset encoding)
            throws Exception {
        Document document = DocumentHelper.parseText(content);
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileWriter(path), format);
        writer.write(document);
        writer.close();
    }
}
